package com.cauli.file.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cauli.file.component.AsyncTaskComp;
import com.cauli.file.model.dto.recovery.BatchDeleteRecoveryFileDTO;
import com.cauli.file.model.dto.recovery.RestoreFileDTO;
import com.cauli.file.model.entity.RecoveryFile;
import com.cauli.file.model.vo.recovery.RecoveryFileListVO;
import com.cauli.file.service.RecoveryFileService;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.qiwenshare.common.anno.MyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Cauli
 * @date 2023-03-18 12:00
 * @description 回收站 前端控制器
 */
@Api(tags = "回收站")
@ApiSort(3)
@RestController
@RequestMapping("/recoveryFile")
public class RecoveryFileController {
    @Autowired
    private RecoveryFileService recoveryFileService;

    @Resource
    private AsyncTaskComp asyncTaskComp;

    public static final String CURRENT_MODULE = "回收站";

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "回收文件列表")
    @GetMapping(value = "/list")
    public ResultResponse<RecoveryFileListVO> getRecoveryFileList() {
        long userId = StpUtil.getLoginIdAsLong();
        List<RecoveryFileListVO> recoveryFileList = recoveryFileService.selectRecoveryFileList(userId);
        return ResultResponse.success().dataList(recoveryFileList, Long.valueOf(recoveryFileList.size()));
    }

    @MyLog(operation = "删除回收文件", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "删除回收文件")
    @DeleteMapping(value = "/deleteRecoveryFile")
    public ResultResponse<String> batchDeleteRecoveryFile(@RequestBody BatchDeleteRecoveryFileDTO batchDeleteRecoveryFileDTO) {
        String userFileIds = batchDeleteRecoveryFileDTO.getUserFileIds();
        String[] userFileIdList = userFileIds.split(",");
        for (String userFileId : userFileIdList) {
            RecoveryFile recoveryFile = recoveryFileService.getOne(new QueryWrapper<RecoveryFile>().lambda().eq(RecoveryFile::getUserFileId, userFileId));

            if (recoveryFile != null) {
                asyncTaskComp.deleteUserFile(recoveryFile.getUserFileId());
                recoveryFileService.removeById(recoveryFile.getRecoveryFileId());
            }
        }
        return ResultResponse.success().message("删除成功");
    }

    @MyLog(operation = "还原文件", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "还原文件")
    @PostMapping(value = "/restoreFile")
    public ResultResponse<?> restoreFile(@RequestBody RestoreFileDTO restoreFileDto) {
        recoveryFileService.restoreFile(restoreFileDto.getDeleteBatchNum(), restoreFileDto.getFilePath(), StpUtil.getLoginIdAsLong());
        return ResultResponse.success().message("还原成功");
    }
}









